<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;

error_reporting(E_ERROR | E_PARSE );
// 应用公共文件

/**
  * 功能：是否是移动端
  *
  * User: cyf
  * Time: 2018/7/3 0003 11:01
  * @return bool
  */
 function isMobile()
 {
     if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
     {
         return true;
     }
     if (isset ($_SERVER['HTTP_VIA']))
     {
         return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
     }
     if (isset ($_SERVER['HTTP_USER_AGENT']))
     {
         $clientkeywords = array ('nokia',
             'sony',
             'ericsson',
             'mot',
             'samsung',
             'htc',
             'sgh',
             'lg',
             'sharp',
             'sie-',
             'philips',
             'panasonic',
             'alcatel',
             'lenovo',
             'iphone',
             'ipod',
             'blackberry',
             'meizu',
             'android',
             'netfront',
             'symbian',
             'ucweb',
             'windowsce',
             'palm',
             'operamini',
             'operamobi',
             'openwave',
             'nexusone',
             'cldc',
             'midp',
             'wap',
             'mobile'
         );
         if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
         {
             return true;
         }
     }
     if (isset ($_SERVER['HTTP_ACCEPT']))
     {
         if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
         {
             return true;
         }
     }
     return false;
 }

// 2021.9.27获取列表
function lists($Db,$w,$field='id,img1,title',$order="disorder desc,id desc"){
    $list = Db::name($Db)->field($field)->where($w)->order($order)->select();
    foreach ($list as $k => $v) {
        if($v['img1']){
            // 检测图片地址
            $list[$k]['img1'] = strpos($v['img1'],'http')===false?$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$v['img1']:$v['img1'];
        }
    }
    return $list;
}

// 2021.9.27获取一条
function get_finds($Db,$w,$field='id,img1,title',$order="disorder desc,id desc"){
    $list = Db::name($Db)->field($field)->where($w)->order($order)->select();
    foreach ($list as $k => $v) {
        if($v['img1']){
            // 检测图片地址
            $list[$k]['img1'] = strpos($v['img1'],'http')===false?$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$v['img1']:$v['img1'];
        }
    }
    return $list[0];
}

//获取BANNER图片名称
function get_banner_imgname($i){
    $imgname=config('BANNER_NAME');
    $arrValue=explode(',',$imgname);
    if($imgname){
        if(count($arrValue)==1){
            $str=$imgname;
        }else{
            $str=$arrValue[$i-1];
        }
    }else{
        $str="配置图片";
    }

    return $str;
}

//获取BANNER图片尺寸
    function get_banner_imgsize($i){
        $imgsize=config('BANNER_SIZE');
        $arrValue=explode(',',$imgsize);
        if(count($arrValue)==1){
            $str=$imgsize;
        }else{
            $str=$arrValue[$i-1];
        }
        return $str;
    }
#文件单位
function formatBytes($size){ 
    $units=array('B','K','M','G','TB'); 
    for($i=0;$size>=1024&&$i<4;$i++)
    {
        $size/=1024;
    }
    return round($size,2).' '.$units[$i]; 
}

//列出文件列表
function deal_arr($data,$root,$name=array()){
    unset($data[0]);unset($data[1]);
    $a=array();
    $b=array();

    foreach ($data as $key=>$val)
    {
        if(is_dir($root.'/'.$val))
        {
            echo "{$val}";
            $a[$key]=array('0'=>iconv("gb2312","utf-8",$val),'1'=>filemtime($root.'/'.$val));
        }
        elseif(is_file($root.'/'.$val))
        {
            $ext=strtolower(strrchr($root.'/'.$val,'.'));
            $b[$key]=array('0'=>iconv("gb2312","utf-8",$val),'1'=>filemtime($root.'/'.$val),'2'=>formatBytes(filesize($root.'/'.$val)),'3'=>$ext,'4'=>is_image($ext));
        }
        else
        {
            unset($data[$key]);
        }
    }
    krsort($a);
    krsort($b);
    return array('0'=>$a,'1'=>$b);
}

//模块名称
function get_model_name($strs=0){
    $str='';
    $v = explode(',',$strs);
    $model_fields=config('model_fields');
    for($i=0;$i<count($v);$i++){
        if($i>0)
        $str.="、".$model_fields[$v[$i]];
        else
        $str.=$model_fields[$v[$i]];
    }
    return $str;
}

//获取指定表的字段
function get_myfields_list($pid){
    $tablename=get_forms_zd($pid);
    $list=Db()->query("SHOW FULL COLUMNS from `tp_{$tablename}`");
    foreach ($list as $k=>$v){
        $arr=explode("_",$v['Comment']);
        // print_r($arr);die;
        $list[$k]['title']=$arr[0];
        // $list[$k]['tip']=$arr[1];
    }
    return $list;
}
//获取指定表的字段
function get_list_myfields_list($pid){
    $tablename=get_forms_zd($pid);
    $list=Db()->query("SHOW FULL COLUMNS from `tp_{$tablename}`");
    //print_r($list);
    foreach ($list as $k=>$v){
        $arr=explode("_",$v['Comment']);
        // print_r($arr);die;
        $list[$k]['title']=$arr[0];
        // $list[$k]['tip']=$arr[1];
        if($v['Field']=='id'||$v['Field']=='ip'||$v['Field']=='status'||$v['Field']=='sendtime'){
            unset($list[$k]);
        }
    }

    return $list;
}
//获取指定表的字段
function get_js_myfields_list($pid){
    $tablename=get_forms_zd($pid);
    $list=Db()->query("SHOW FULL COLUMNS from `tp_{$tablename}`");
    foreach ($list as $k=>$v){
        $arr=explode("_",$v['Comment']);
        // $list[$k]['tip']=$arr[1];
        // if($arr[1]){
        //     $list[$k]['js']="if(checkspaces(formlist.{$v['Field']}.value)){
        //                         alert('{$arr[1]}');
        //                         formlist.{$v['Field']}.focus();
        //                         return false;
        //                      }\n";
        // }else{
            $list[$k]['js']='';
        // }

    }
    //print_r($list);
    return $list;
}

//获取图片尺寸
function get_imgsize($i,$pid,$ty,$tty){
    if($tty>0) $id=$tty;
    elseif($ty>0) $id=$ty;
    elseif($pid>0) $id=$pid;
    $imgsize=get_sort_zd($id,'imgsize');
    $arrValue=explode(',',$imgsize);
    if(count($arrValue)==1){
        $str=$imgsize;
    }else{
        $str=$arrValue[$i-1];
    }
    return $str;
}